package com.dcm.bargain.service.impl;

import com.dcm.bargain.entity.BargainEntity;
import com.dcm.bargain.mapper.BargainMapper;
import com.dcm.bargain.service.BargainService;
import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:zhouyuhan
 * 时间: 2021/3/27 15:34
 */
@Service
public class BargainServiceImpl implements BargainService {

    @Autowired
    private BargainMapper bargainMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<BargainEntity> findAll(){
        return bargainMapper.findAll();
    }

    /**
     * 根据id删除
     * @param id
     * @throws MyException
     */
    @Override
    public void delete(Long id)throws MyException {
        if (id==null){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ID_CANNOT_NULL);
        }
        bargainMapper.delete(id);
    }

    /**
     * 模糊姓名和id
     * @param id
     * @param bargainName
     * @return
     * @throws MyException
     */
    @Override
    public List<BargainEntity> findbargainByIdAndName(Long id, String bargainName) throws MyException{
        if (id==null){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ID_CANNOT_NULL);
        }
        if (StringUtils.isEmpty(bargainName)){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_BARGAINNAME_CANNOT_NULL);
        }
        return bargainMapper.findbargainByIdAndName(id,bargainName);
    }

    /**
     * 添加
     * @param bargainEntity
     * @throws MyException
     */
    @Override
    public void add(BargainEntity bargainEntity)throws MyException {
        checkBargain(bargainEntity);
        bargainMapper.add(bargainEntity);
    }

    /**
     * 修改
     * @param bargainEntity
     * @throws MyException
     */
    @Override
    public void update(BargainEntity bargainEntity)throws MyException {
        checkBargain(bargainEntity);
        bargainMapper.update(bargainEntity);
    }

    /**
     * 回显
     * @param id
     * @return
     * @throws MyException
     */
    @Override
    public BargainEntity echo(Long id)throws MyException {
        if (id==null){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ID_CANNOT_NULL);
        }
        return bargainMapper.echo(id);
    }

    /**
     * 分页
     * @param pageSize
     * @param currentPage
     * @return
     * @throws MyException
     */
    @Override
    public PageInfo<BargainEntity> findPage(Integer pageSize, Integer currentPage) throws MyException{
        if (pageSize==null|| "".equals(pageSize)){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_PAGESIZE_CANNOT_NULL);
        }
        if (currentPage==null|| "".equals(currentPage)){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_CURRENTPAGE_CANNOT_NULL);
        }
        PageHelper.startPage(currentPage,pageSize);
        List<BargainEntity> all = bargainMapper.findAll();
        PageInfo<BargainEntity> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    /**
     * 通过id修改状态
     * @param id
     * @param status
     * @throws MyException
     */
    @Override
    public void updateStatus(Long id, Integer status) throws MyException{
        if (id==null|| "".equals(id)){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ID_CANNOT_NULL);
        }
        if (status==null|| "".equals(status)){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_STATUS_CANNOT_NULL);
        }
        bargainMapper.updateStatus(id,status);
    }

    /**
     * 静态调用方法
     * @param bargainEntity
     */
    public static void checkBargain(BargainEntity bargainEntity){
        if (StringUtils.isEmpty(bargainEntity.getBargainName())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_BARGAINNAME_CANNOT_NULL);
        }
        if (StringUtils.isEmpty(bargainEntity.getImages())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_IMAGES_CANNOT_NULL);
        }
        if (bargainEntity.getBargainPrice()==null || "".equals(bargainEntity.getBargainPrice())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_BARGAINPRICE_CANNOT_NULL);
        }
        if (bargainEntity.getLowPrice()==null || "".equals(bargainEntity.getLowPrice())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_LOWPRICE_CANNOT_NULL);
        }
        if (bargainEntity.getPerson()==null || "".equals(bargainEntity.getPerson())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_PERSON_CANNOT_NULL);
        }
        if (bargainEntity.getBargainPerson()==null || "".equals(bargainEntity.getBargainPerson())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_BARGAINPERSON_CANNOT_NULL);
        }
        if (bargainEntity.getSuccessPerson()==null || "".equals(bargainEntity.getSuccessPerson())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_SUCCESS_PERSON_CANNOT_NULL);
        }
        if (bargainEntity.getAstrict()==null || "".equals(bargainEntity.getAstrict())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ASTRICT_CANNOT_NULL);
        }
        if (bargainEntity.getAstrictResidue()==null || "".equals(bargainEntity.getAstrictResidue())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ASTRICTRESIDUE_CANNOT_NULL);
        }
        if (bargainEntity.getEndTime()==null || "".equals(bargainEntity.getEndTime())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_ENDTIME_CANNOT_NULL);
        }
        if (bargainEntity.getStatus()==null || "".equals(bargainEntity.getStatus())){
            throw new MyException(Constants.BargainErrorMsg.BARGAIN_STATUS_CANNOT_NULL);
        }
    }
}
